﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;
using netDxf;
using netDxf.Blocks;
using netDxf.Collections;
using netDxf.Entities;
using netDxf.Header;
using netDxf.Objects;
using netDxf.Tables;
using netDxf.Units;
using Attribute = netDxf.Entities.Attribute;
using Image = netDxf.Entities.Image;
using Point = netDxf.Entities.Point;
using Trace = netDxf.Entities.Trace;
using System.Threading;

namespace TestDxfDocument
{

    class _ShellReamer222
    {
        public static void ShellReamer222(Dictionary<string, Object> data)  //套式铰刀
        {
            DxfDocument dxf = new DxfDocument();
            //图层
            Layer contour = new Layer("contour") { Color = AciColor.Cyan };    //轮廓层
            Layer dimensioning = new Layer("dimensioning");    //尺寸层
            Layer refin = new Layer("refin") { Color = AciColor.Red };    //基准线/内轮廓
            Layer centre = new Layer("centre") { Color = AciColor.Yellow };    //中心线
            Layer textlayer = new Layer("textlayer") { Color = AciColor.Blue };    //文本层
            Layer hatching = new Layer("hatching") { Color = AciColor.Blue };    //填充层
            Layer additionalline = new Layer("additionalline") { Color = AciColor.Cyan };    //附加线
            Layer cutting = new Layer("cutting") { Color = AciColor.Red };    //切削层
            Layer nocutting = new Layer("nocutting");    //非切削层
            Layer skcontour = new Layer("skcontour") { Color = AciColor.Cyan };    //sk轮廓层
            Layer skdimensioning = new Layer("skdimensioning");    //sk尺寸线层
            Layer skrefin = new Layer("skrefin") { Color = AciColor.Red };    //sk基准线/内轮廓
            Layer sktext = new Layer("sktext") { Color = AciColor.Blue };    //sk文本
            Layer skhatching = new Layer("skhatching") { Color = AciColor.Blue };    //sk填充
            Layer skadditionlline = new Layer("skadditionlline") { Color = AciColor.Cyan };    //sk附加线
            Layer skviewcontour = new Layer("skviewcontour") { Color = AciColor.Cyan };    //skview轮廓
            Layer skviewdimensioning = new Layer("skviewdimensioning");    //skview尺寸线层
            //作图
            //参数
            /*double soal = Convert.ToDouble(data["oal"]);
            double scale = Convert.ToDouble(data["scale"]); //放大系数*/
            double soal = 60;
            double scale = 1;
            if (scale == 0 && soal < 67)
            {
                scale = 4.0;
            }
            else if (scale == 0 && soal < 90)
            {
                scale = 3.0;
            }
            else if (scale == 0 && soal < 134)
            {
                scale = 2.0;
            }
            else if (scale == 0 && soal < 178)
            {
                scale = 1.5;
            }
            else if (scale == 0 && soal < 268)
            {
                scale = 1.0;
            }
            else if (scale == 0 && soal < 357)
            {
                scale = 0.75;
            }
            else if (scale == 0)
            {
                scale = 0.5;
            }
            double pi = Math.PI;
            string path = Convert.ToString(data["path"]);

           /* double dc = Convert.ToDouble(data["dc"]) * scale; //Convert.ToDouble(data["dc"]);
            double apmx = Convert.ToDouble(data["l"]) * scale; //Convert.ToDouble(data["l"]);
            double oal = Convert.ToDouble(data["oal"]) * scale; //Convert.ToDouble(data["oal"]);
            double dmm = Convert.ToDouble(data["dconms"]) * scale; //Convert.ToDouble(data["czcms"]);
            double plgang = Convert.ToDouble(data["plgang"]) * pi / 180; //Convert.ToDouble(data["plgang"]);
            double isframe = Convert.ToDouble(data["isframe"]);*/

            double dc = 49;
            double apmx = 40;
            double oal = 60;
            double dmm = 23;
            double plgang = 30 * pi / 180;
            double isframe = 0;

            double df = dmm * 1.4;
            double plgl = apmx / 8;
            double lf = oal - plgl; //Convert.ToDouble(data["lf"]);
            //坐标
            Vector2 p00 = new Vector2(0, 0);  //vector2 是二维的点
            Vector2 p01 = fun.Polar(p00, 0.5 * pi, oal);  // 基准点 角度（弧度制） 距离      
            Vector2 p5 = fun.Polar(p00, 0, df / 2);
            Vector2 p4 = new Vector2(p00.X + dc / 2, p00.Y + plgl);
            Vector2 p3 = new Vector2(p4.X, p4.Y + apmx - plgl);
            Vector2 p1 = new Vector2(p5.X, p5.Y + oal);
            Vector2 p19 = new Vector2(p1.X - (0.9 * df - dmm), p1.Y);
            Vector2 p2 = new Vector2(p5.X, p3.Y + plgl);
            Vector2 p18 = new Vector2(p19.X, p2.Y + 3);
            Vector2 p17 = new Vector2(p18.X - dmm / 10, p18.Y);
            Vector2 p16 = new Vector2(p00.X, p17.Y);
            Vector2 p15 = new Vector2(p00.X - df / 2, p1.Y);
            Vector2 p9 = new Vector2(p00.X - dc / 2, p4.Y);
            Vector2 p12 = new Vector2(p9.X, p00.Y + apmx);
            Vector2 p14 = new Vector2(p15.X, p16.Y);
            Vector2 p13 = new Vector2(p14.X, p14.Y - plgl / 2);
            Vector2 p11 = new Vector2(p12.X + plgl / 2, p12.Y);
            Vector2 p10 = new Vector2(p9.X + plgl / 2, p9.Y);
            Vector2 p8 = new Vector2(p9.X + 0.5 * plgl / Math.Tan(plgang), p00.Y);
            Vector2 p7 = new Vector2(p8.X + plgl / 2, p8.Y);
            Vector2 p6 = new Vector2(p17.X - p17.Y / 60, p00.Y);
            Vector2 p31 = new Vector2(p00.X + df / 6, p1.Y);
            Vector2 p32 = new Vector2(p31.X - df / 15, p31.Y - df / 15);
            Vector2 p33 = new Vector2(p32.X, p32.Y - df / 8);
            Vector2 p34 = new Vector2(p33.X - df / 15, p33.Y - df / 15);
            Vector2 p38 = new Vector2(p00.X - df / 6, p1.Y);
            Vector2 p37 = new Vector2(p38.X + df / 15, p32.Y);
            Vector2 p36 = new Vector2(p37.X, p32.Y - df / 8);
            Vector2 p35 = new Vector2(p36.X + df / 15, p36.Y - df / 15);
            Vector2 p50 = new Vector2(p5.X + 0.2 * plgl / Math.Tan(plgang), p5.Y + apmx / 3);
            Vector2 p55 = new Vector2(p5.X, p50.Y + apmx / 9 - Math.Sqrt(apmx * apmx / 81 - (p50.X - p5.X) * (p50.X - p5.X)));
            Vector2 p51 = new Vector2(p50.X, p50.Y + apmx / 3);
            Vector2 p52 = new Vector2(p55.X, p51.Y - p55.Y + p50.Y);
            Vector2 p53 = new Vector2(p51.X - apmx / 9, p51.Y - apmx / 9);
            Vector2 p54 = new Vector2(p53.X, p50.Y + apmx / 9);
            Vector2 p56 = new Vector2(p5.X - 0.6 * plgl / Math.Tan(plgang), p5.Y);
            Vector2 p59 = new Vector2(p5.X, p5.Y + apmx);
            Vector2 p58 = new Vector2(p59.X - 0.4 * plgl / Math.Tan(plgang), p59.Y);
            Vector2 p57 = new Vector2(p59.X - 0.6 * plgl / Math.Tan(plgang), p59.Y - 0.2 * plgl / Math.Tan(plgang));
            Vector2 v18 = new Vector2(p19.X, p34.Y - (p34.Y - p58.Y) / 2);
            Vector2 v17 = new Vector2(v18.X - dmm / 10, v18.Y);
            Vector2 v16 = new Vector2(p00.X, v17.Y);

            //线段
            LwPolyline poly = new LwPolyline();
            poly.Vertexes.Add(new LwPolylineVertex(p1));
            poly.Vertexes.Add(new LwPolylineVertex(p2));
            poly.Vertexes.Add(new LwPolylineVertex(p3));
            poly.Vertexes.Add(new LwPolylineVertex(p4));
            poly.Vertexes.Add(new LwPolylineVertex(p5));
            poly.Vertexes.Add(new LwPolylineVertex(p6));
            poly.Vertexes.Add(new LwPolylineVertex(p7));
            poly.Vertexes.Add(new LwPolylineVertex(p8));
            poly.Vertexes.Add(new LwPolylineVertex(p9));
            poly.Vertexes.Add(new LwPolylineVertex(p12));
            poly.Vertexes.Add(new LwPolylineVertex(p14));
            poly.Vertexes.Add(new LwPolylineVertex(p15));
            poly.Vertexes.Add(new LwPolylineVertex(p38));
            poly.Vertexes[12].Bulge = Math.Tan(pi / (-8));
            poly.Vertexes.Add(new LwPolylineVertex(p37));
            poly.Vertexes.Add(new LwPolylineVertex(p36));
            poly.Vertexes[14].Bulge = Math.Tan(pi / 8);
            poly.Vertexes.Add(new LwPolylineVertex(p35));
            poly.Vertexes.Add(new LwPolylineVertex(p34));
            poly.Vertexes[16].Bulge = Math.Tan(pi / 8);
            poly.Vertexes.Add(new LwPolylineVertex(p33));
            poly.Vertexes.Add(new LwPolylineVertex(p32));
            poly.Vertexes[18].Bulge = Math.Tan(pi / (-8));
            poly.Vertexes.Add(new LwPolylineVertex(p31));
            poly.Vertexes.Add(new LwPolylineVertex(p1));
            fun.PolyFill(dxf, poly, 0.3, contour);
            Vector2[] Line1 = { p1, p52 };
            fun.drawBLine(dxf, Line1, 0.3, contour);
            Vector2[] Line2 = { p53, p54 };
            fun.drawBLine(dxf, Line2, 0.3, contour);
            LwPolyline Arc3 = new LwPolyline();
            Arc3.Vertexes.Add(new LwPolylineVertex(p51));
            Arc3.Vertexes.Add(new LwPolylineVertex(p53));
            Arc3.Vertexes[0].Bulge = (1 - Math.Cos(pi / 4)) * Math.Cos(pi / 4) * 2;
            fun.PolyFill(dxf, Arc3, 0.3, contour);
            LwPolyline Arc4 = new LwPolyline();
            Arc4.Vertexes.Add(new LwPolylineVertex(p54));
            Arc4.Vertexes.Add(new LwPolylineVertex(p50));
            Arc4.Vertexes[0].Bulge = (1 - Math.Cos(pi / 4)) * Math.Cos(pi / 4) * 2;
            fun.PolyFill(dxf, Arc4, 0.3, contour);
            Vector2[] dotline1 = { fun.Polar(p01, pi / 2, 4), fun.Polar(p00, 1.5 * pi, 4) };
            fun.drawPlinetype(dxf, dotline1, 0.15, centre, Linetype.Center);
            Vector2[] xiline1 = { p7, p10, p11, p13, p14 };
            fun.drawPlineOfw(dxf, xiline1, 0.3, contour);
            Vector2[] xiline2 = { p5, p55 };
            fun.drawPlineOfw(dxf, xiline2, 0.3, contour);
            Vector2[] xiline3 = { p2, p1, p19, v18, v17, p6 };
            fun.drawPlineOfw(dxf, xiline3, 0.3, contour);
            Vector2[] xiline4 = { v16, v17 };
            fun.drawPlineOfw(dxf, xiline4, 0.3, contour);
            //填充1
            LwPolyline poly3 = new LwPolyline();
            poly3.Vertexes.Add(new LwPolylineVertex(p1));
            poly3.Vertexes.Add(new LwPolylineVertex(p2));
            poly3.Vertexes.Add(new LwPolylineVertex(p59));
            poly3.Vertexes.Add(new LwPolylineVertex(p58));
            poly3.Vertexes[3].Bulge = Math.Tan(pi / (8));
            poly3.Vertexes.Add(new LwPolylineVertex(p57));
            poly3.Vertexes.Add(new LwPolylineVertex(p56));
            poly3.Vertexes.Add(new LwPolylineVertex(p6));
            poly3.Vertexes.Add(new LwPolylineVertex(v17));
            poly3.Vertexes.Add(new LwPolylineVertex(v18));
            poly3.Vertexes.Add(new LwPolylineVertex(p19));
            poly3.SetConstantWidth(0.3);
            poly3.IsClosed = true;
            String[] type = new String[100];
            Line[] line = new Line[100];
            Arc[] arc2 = new Arc[100];
            int numst = poly3.Explode().Count;  //记录爆炸后，线段有多少
            for (int i = 0; i < numst; i++)
            {
                type[i] = poly3.Explode()[i].Type.ToString();    //将所有的元素记录成一个数组
                if (type[i] == "Line")
                {
                    line[i] = (Line)poly3.Explode()[i];
                }
                if (type[i] == "Arc")
                {
                    arc2[i] = (Arc)poly3.Explode()[i];
                }
            }
            fun.hatchsub1(dxf, type, numst, 20, 15, line, arc2, hatching);
            fun.PolyFill(dxf, poly3, 0.3, contour);
            Vector2[] Line3 = { p50, p51 };
            fun.drawBLine(dxf, Line3, 0.3, contour);
            //填充2
            LwPolyline poly4 = new LwPolyline();
            poly4.Vertexes.Add(new LwPolylineVertex(p4));
            poly4.Vertexes.Add(new LwPolylineVertex(p3));
            poly4.Vertexes.Add(new LwPolylineVertex(p2));
            poly4.Vertexes.Add(new LwPolylineVertex(p52));
            poly4.Vertexes[3].Bulge = -Math.Tan(Math.Asin((p51.X - p52.X) * 9 / apmx) / 4);
            poly4.Vertexes.Add(new LwPolylineVertex(p51));
            poly4.Vertexes.Add(new LwPolylineVertex(p50));
            poly4.Vertexes[5].Bulge = -Math.Tan(Math.Asin((p51.X - p52.X) * 9 / apmx) / 4);
            poly4.Vertexes.Add(new LwPolylineVertex(p55));
            poly4.Vertexes.Add(new LwPolylineVertex(p5));
            poly4.SetConstantWidth(0.3);
            poly4.IsClosed = true;
            String[] type3 = new String[100];
            Line[] line3 = new Line[100];
            Arc[] arc3 = new Arc[100];
            int numst1 = poly4.Explode().Count;  //记录爆炸后，线段有多少
            for (int i = 0; i < numst1; i++)
            {
                type3[i] = poly4.Explode()[i].Type.ToString();    //将所有的元素记录成一个数组
                if (type3[i] == "Line")
                {
                    line3[i] = (Line)poly4.Explode()[i];
                }
                if (type3[i] == "Arc")
                {
                    arc3[i] = (Arc)poly4.Explode()[i];
                }
            }
            fun.hatchsub1(dxf, type3, numst1, 20, 12, line3, arc3, hatching);
            fun.PolyFill(dxf, poly4, 0.3, contour);
            //标注样式
            //箭头样式
            DimensionStyle dimensionStyle = fun.CreateDimStyle();
            dimensionStyle.ArrowSize = 3.5;
            dimensionStyle.DimArrow1 = DimensionArrowhead.Closedfilled;
            dimensionStyle.DimArrow2 = DimensionArrowhead.Closedfilled;
            //文本样式
            dimensionStyle.TextHeight = 3.5;
            double offset = 10;
            //线段尺寸标注
            //尺寸1
            LinearDimension dim1 = new LinearDimension(new Line(p1, p5), offset * 3 + p3.X - p1.X, 270, dimensionStyle);
            dim1.UserText = "OAL = " + Math.Round(oal / scale, 2);
            dxf.AddEntity(dim1);
            //尺寸2
            LinearDimension dim2 = new LinearDimension(new Line(p3, p5), offset * 2 + (p3.X - p5.X) / 2, 270, dimensionStyle);
            dim2.UserText = "L = " + Math.Round(apmx / scale, 2);
            dxf.AddEntity(dim2);
            //尺寸5
            LinearDimension dim5 = new LinearDimension(new Line(p9, p4), offset * 2, 180, dimensionStyle);
            dim5.UserText = "DC = " + Math.Round(dc / scale, 2);
            dxf.AddEntity(dim5);
            //尺寸8
            LinearDimension dim11 = new LinearDimension(new Line(new Vector2(-v17.X, v17.Y), v17), offset + p1.Y - p17.Y, 0, dimensionStyle);
            dim11.UserText = "DCONMS = " + Math.Round(dmm / scale, 2);
            dxf.AddEntity(dim11);
            //图框
            if (isframe == 1)
            {
                Dictionary<string, Vector2> position = Frame.frameA3Y(dxf, new Vector2(-148.5, 200 + oal / 2));
                fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
            }
            //定义格式，根据路径导出
            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2007;
            dxf.Save(path);
        }

    }
}
